//
//  EPMADTableViewCell.m
//  myXianfeng
//
//  Created by 马继鵬 on 17/4/27.
//  Copyright © 2017年 EPM. All rights reserved.
//

#import "EPMADTableViewCell.h"
#import "EPMHomeCategoryModel.h"


@interface EPMADTableViewCell()<SDCycleScrollViewDelegate>
@property (weak,nonatomic) SDCycleScrollView* cycleScrollView;

@end

@implementation EPMADTableViewCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]){
        [self setupUI];
    }
    return self;
}

- (void)setupUI{
    
    [self addCycleView];
    
    [self addBtns];
    
}

- (void)addCycleView{
//    SDCycleScrollView* cycleView = [SDCycleScrollView cycleScrollViewWithFrame: CGRectMake(0, 0, ScreenWidth, 200) imageNamesGroup:@[@"lbq-1",@"lbq-2",@"lbq-3"]];
////    cycleView.backgroundColor = [UIColor redColor];
//    self.cycleScrollView = cycleView;
//    [self addSubview:cycleView];
//    cycleView.delegate = self;
    
    CGFloat width = ScreenWidth;
    
    SDCycleScrollView* cycleScrollView = [[SDCycleScrollView  alloc]initWithFrame:CGRectMake(0,0, width, 180)];
    cycleScrollView.delegate = self;
    cycleScrollView.placeholderImage = [UIImage imageNamed:@"v2_placeholder_full_size"];
    self.cycleScrollView = cycleScrollView;
    
    NSArray* imgNameList = @[@"lbq-1",@"lbq-2",@"lbq-3"];
    cycleScrollView.localizationImageNamesGroup = imgNameList;
    cycleScrollView.pageControlAliment = SDCycleScrollViewPageContolAlimentRight;
    cycleScrollView.autoScrollTimeInterval = 2.0;
    
    self.cycleScrollView = cycleScrollView;
    [self addSubview:cycleScrollView];

}



- (void)addBtns{
    
    CGFloat btnMargine = 40;
    CGFloat btnWidth = (ScreenWidth - btnMargine * 5)/4;
    CGFloat btnHeight = btnWidth* 5 / 4;
    CGFloat btnY = 200 + 7;
       NSArray* ADList = _HomeManager.categoryList;
   
    
    for (int i = 0; i < 4; i ++){
        CGFloat btnX = btnMargine* (i +1) + btnWidth * i;
        EPMHomeCategoryModel* model = ADList[i];
        UIControl* control = [self controlWithImg:model.img title:model.name width:btnWidth];
        control.frame = CGRectMake(btnX, btnY, btnWidth, btnHeight);
        [self.contentView addSubview:control];
        control.tag = i+1;
        [control addTarget:self action:@selector(actionBtnClick:) forControlEvents:UIControlEventTouchUpInside];
    }
}

#pragma mark - 点击事件 -

- (void)actionBtnClick:(UIControl*)sender{
    if (self.categoryDidClickBlock){
        self.categoryDidClickBlock(sender.tag);
    }
}

- (UIControl*) controlWithImg:(NSString*)imgName title:(NSString*)title width:(CGFloat)width{
    
    UIControl* control = [[UIControl alloc]init];
    UIImageView* imgView = [[UIImageView alloc]init];
    [imgView sd_setImageWithURL:[NSURL URLWithString:imgName]];
    
    
    [control addSubview:imgView];
    [imgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.top.equalTo(control);
        make.width.mas_equalTo(width);
        //修改图片大小
        make.height.mas_equalTo(0.8*width);
    }];
//    imgView.layer.masksToBounds = YES;
//    imgView.layer.cornerRadius = width/2;
    
    UILabel* label = [[UILabel alloc]init];
    label.textAlignment = NSTextAlignmentCenter;
    label.font = [UIFont systemFontOfSize:13];
    label.textColor = [UIColor darkGrayColor];
    label.text = title;
    [label sizeToFit];
    [control addSubview:label];
    [label mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(control);
        make.top.equalTo(imgView.mas_bottom).offset(7);
       
    }];
    
    return control;
}

- (void)cycleScrollView:(SDCycleScrollView *)cycleScrollView didSelectItemAtIndex:(NSInteger)index{
    
}



@end
